from fastapi import APIRouter, Depends
import base64
import qrcode
from io import BytesIO

waste = APIRouter(tags=['危险废物'])


@waste.post('/waste_code', summary='生成危险废物二维码')
async def create_code(data: str):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    # 添加数据
    qr.add_data(data)
    qr.make(fit=True)

    # 创建一个字节缓冲区并生成二维码图像
    img = qr.make_image(fill_color="black", back_color="white")

    # 将二维码图像保存到字节缓冲区
    buffered = BytesIO()
    img.save(buffered, "PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')

    return img_str
